package com.mlink.ai.chat.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import com.mlink.ai.chat.assistant.robot.R
import com.mlink.ai.chat.assistant.robot.databinding.LayoutMainBottomNavigationBinding


enum class BottomNavigationTab {
    TAB_EXPLORE,
    TAB_CHAT,
    TAB_RECENTS
}

const val KEY_SELECTED_TAB = "key_selected_tab" // 0 explore , 1 chat , 2 recent

typealias BottomTabClickListener = (BottomNavigationTab) -> Unit

class ChatBottomNavigationView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    val binding: LayoutMainBottomNavigationBinding
    var bottomTabClickListener: BottomTabClickListener? = null

    init {
        binding = LayoutMainBottomNavigationBinding.inflate(LayoutInflater.from(context))
        addView(
            binding.root,
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        binding.rlExplore.setOnClickListener {
            setSelectedTab(BottomNavigationTab.TAB_EXPLORE)
            bottomTabClickListener?.invoke(BottomNavigationTab.TAB_EXPLORE)
        }

        binding.rlChat.setOnClickListener {
            setSelectedTab(BottomNavigationTab.TAB_CHAT)
            bottomTabClickListener?.invoke(BottomNavigationTab.TAB_CHAT)
        }

        binding.rlRecents.setOnClickListener {
            setSelectedTab(BottomNavigationTab.TAB_RECENTS)
            bottomTabClickListener?.invoke(BottomNavigationTab.TAB_RECENTS)
        }
    }

    fun performClickTab(tab: BottomNavigationTab) {
        when (tab) {
            BottomNavigationTab.TAB_EXPLORE -> binding.rlExplore.performClick()
            BottomNavigationTab.TAB_CHAT -> binding.rlChat.performClick()
            BottomNavigationTab.TAB_RECENTS -> binding.rlRecents.performClick()
        }
    }

    private fun setSelectedTab(tab: BottomNavigationTab) {
        binding.run {
            ivExplore.setImageResource(
                if (tab == BottomNavigationTab.TAB_EXPLORE) {
                    R.drawable.ic_tab_explore_select
                } else {
                    R.drawable.ic_tab_explore_unselect
                }
            )
            tvExplore.setTextAppearance(
                context,
                if (tab == BottomNavigationTab.TAB_EXPLORE) {
                    R.style.BottomTabSelect
                } else {
                    R.style.BottomTabUnselect
                }
            )

            ivChat.setImageResource(
                if (tab == BottomNavigationTab.TAB_CHAT) {
                    R.drawable.ic_tab_chat_normal
                } else {
                    R.drawable.ic_tab_chat_gray
                }
            )
            tvChat.setTextAppearance(
                context,
                if (tab == BottomNavigationTab.TAB_CHAT) {
                    R.style.BottomTabSelect
                } else {
                    R.style.BottomTabUnselect
                }
            )

            ivRecents.setImageResource(
                if (tab == BottomNavigationTab.TAB_RECENTS) {
                    R.drawable.ic_tab_setting_selected
                } else {
                    R.drawable.ic_tab_setting_normal
                }
            )
            tvRecents.setTextAppearance(
                context,
                if (tab == BottomNavigationTab.TAB_RECENTS) {
                    R.style.BottomTabSelect
                } else {
                    R.style.BottomTabUnselect
                }
            )
        }
    }


}